00001 /* 00002 * Copyright (c) 2013 Battelle Memorial Institute 00003 * Licensed under modified BSD License. A copy of this license can be found 00004 * in the LICENSE file in the top level directory of this distribution. 00005 */ 00006 // ------------------------------------------------------------- 00007 /** 00008 * @file pf_app_module.hpp 00009 * @author Bruce Palmer 00010 * @date 2014-01-28 11:30:49 d3g096 00011 * 00012 * @brief 00013 * 00014 * 00015 */ 00016 // ------------------------------------------------------------- 00017 00018 #ifndef _pf_app_module_h_ 00019 #define _pf_app_module_h_ 00020 00021 #include "boost/smart_ptr/shared_ptr.hpp" 00022 #include "gridpack/serial_io/serial_io.hpp" 00023 #include "gridpack/configuration/configuration.hpp" 00024 #include "pf_factory_module.hpp" 00025 00026 namespace gridpack { 00027 namespace powerflow { 00028 00029 // Structs that are used for some applications 00030 00031 struct pathBranch{ 00032 int fromBus; 00033 int toBus; 00034 std::string branchID; 00035 }; 00036 00037 struct stressBus{ 00038 int busID; 00039 std::string genID; 00040 double participation; 00041 }; 00042 00043 struct pathStress{ 00044 std::string name; 00045 std::vector<pathBranch> path; 00046 std::vector<stressBus> sourceArea; 00047 std::vector<stressBus> sinkArea; 00048 }; 00049 00050 // Contingency types 00051 enum ContingencyType{Generator, Branch}; 00052 00053 // Struct that is used to define a collection of contingencies 00054 00055 struct Contingency 00056 { 00057 int p_type; 00058 std::string p_name; 00059 // Line contingencies 00060 std::vector<int> p_from; 00061 std::vector<int> p_to; 00062 std::vector<std::string> p_ckt; 00063 // Status of line before contingency 00064 std::vector<bool> p_saveLineStatus; 00065 // Generator contingencies 00066 std::vector<int> p_busid; 00067 std::vector<std::string> p_genid; 00068 // Status of generator before contingency 00069 std::vector<bool> p_saveGenStatus; 00070 }; 00071 00072 // Calling program for powerflow application 00073 00074 class PFAppModule 00075 { 00076 public: 00077 /** 00078 * Basic constructor 00079 */ 00080 PFAppModule(void); 00081 00082 /** 00083 * Basic destructor 00084 */ 00085 ~PFAppModule(void); 00086 00087 /** 00088 * Read in and partition the powerflow network. The input file is read 00089 * directly from the Powerflow block in the configuration file so no 00090 * external file names or parameters need to be passed to this routine 00091 * @param network pointer to a PFNetwork object. This should not have any 00092 * buses or branches defined on it. 00093 * @param config point to open configuration file 00094 */ 00095 void readNetwork(boost::shared_ptr<PFNetwork> &network, 00096 gridpack::utility::Configuration *config); 00097 00098 /** 00099 * Set up exchange buffers and other internal parameters and initialize 00100 * network components using data from data collection 00101 */ 00102 void initialize(); 00103 00104 /** 00105 * Reinitialize calculation from data collections 00106 */ 00107 void reload(); 00108 00109 /** 00110 * Execute the iterative solve portion of the application using a hand-coded 00111 * Newton-Raphson solver 00112 * @return false if an error was caught in the solution algorithm 00113 */ 00114 bool solve(); 00115 00116 /** 00117 * Execute the iterative solve portion of the application using a library 00118 * non-linear solver 00119 * @return false if an error was caught in the solution algorithm 00120 */ 00121 bool nl_solve(); 00122 00123 /** 00124 * Write out results of powerflow calculation to standard output 00125 * Separate calls for writing only data from buses or branches 00126 * @param signal tell underlying write what records to print 00127 */ 00128 void write(); 00129 void writeBus(const char* signal); 00130 void writeBranch(const char* signal); 00131 void writeCABus(); 00132 void writeCABranch(); 00133 void writeHeader(const char *msg); 00134 std::vector<std::string> writeBusString(const char *signal = NULL); 00135 std::vector<std::string> writeBranchString(const char *signal = NULL); 00136 00137 /** 00138 * Redirect output from standard out 00139 * @param filename name of file to write results to 00140 */ 00141 void open(const char *filename); 00142 void close(); 00143 00144 /** 00145 * Print string. This can be used to direct output to the file opened using 00146 * the open command 00147 * @param buf string to be printed 00148 */ 00149 void print(const char *buf); 00150 00151 /** 00152 * Save results of powerflow calculation to data collection objects 00153 */ 00154 void saveData(); 00155 00156 /** 00157 * Export final configuration to PSS/E formatted file 00158 * @param filename name of file to store network configuration 00159 */ 00160 void exportPSSE33(std::string &filename); 00161 00162 /** 00163 * Set a contingency 00164 * @param event data describing location and type of contingency 00165 * @return false if location of contingency is not found in network 00166 */ 00167 bool setContingency(Contingency &event); 00168 00169 /** 00170 * Return system to the state before the contingency 00171 * @param event data describing location and type of contingency 00172 * @return false if location of contingency is not found in network 00173 */ 00174 bool unSetContingency(Contingency &event); 00175 00176 /** 00177 * Set voltage limits on all buses 00178 * @param Vmin lower bound on voltages 00179 * @param Vmax upper bound on voltages 00180 */ 00181 void setVoltageLimits(double Vmin, double Vmax); 00182 00183 /** 00184 * Check to see if there are any voltage violations in the network. 00185 * @param area only check for violations in specified area 00186 * @return true if no violations found 00187 */ 00188 bool checkVoltageViolations(); 00189 bool checkVoltageViolations(int area); 00190 00191 /** 00192 * Set "ignore" parameter on all buses with violations so that subsequent 00193 * checks are not counted as violations 00194 */ 00195 void ignoreVoltageViolations(); 00196 00197 /** 00198 * Clear "ignore" parameter on all buses 00199 */ 00200 void clearVoltageViolations(); 00201 00202 /** 00203 * Check to see if there are any line overload violations in 00204 * the network. The last call checks for overloads on specific lines. 00205 * @param area only check for violations in specified area 00206 * @param bus1 original index of "from" bus for branch 00207 * @param bus2 original index of "to" bus for branch 00208 * @param tags line IDs for individual lines 00209 * @param violations true if violation detected on branch, false otherwise 00210 * @return true if no violations found 00211 */ 00212 bool checkLineOverloadViolations(); 00213 bool checkLineOverloadViolations(int area); 00214 bool checkLineOverloadViolations(std::vector<int> &bus1, 00215 std::vector<int> &bus2, std::vector<std::string> &tags, 00216 std::vector<bool> &violations); 00217 00218 /** 00219 * Set "ignore" paramter on all lines with violations so that subsequent 00220 * checks are not counted as violations 00221 */ 00222 void ignoreLineOverloadViolations(); 00223 00224 /** 00225 * Clear "ignore" parameter on all lines 00226 */ 00227 void clearLineOverloadViolations(); 00228 00229 /** 00230 * Check to see if there are any Q limit violations in the network 00231 * @param area only check for violations in specified area 00232 * @return true if no violations found 00233 */ 00234 bool checkQlimViolations(); 00235 bool checkQlimViolations(int area); 00236 00237 /** 00238 * Clear changes that were made for Q limit violations and reset 00239 * system to its original state 00240 */ 00241 void clearQlimViolations(); 00242 00243 /** 00244 * Reset voltages to values in network configuration file 00245 */ 00246 void resetVoltages(); 00247 00248 /** 00249 * Scale generator real power. If zone less than 1 then scale all 00250 * generators in the area. 00251 * @param scale factor to scale real power generation 00252 * @param area index of area for scaling generation 00253 * @param zone index of zone for scaling generation 00254 */ 00255 void scaleGeneratorRealPower(double scale, int area, int zone); 00256 00257 /** 00258 * Scale load power. If zone less than 1 then scale all 00259 * loads in the area. 00260 * @param scale factor to scale load real power 00261 * @param area index of area for scaling load 00262 * @param zone index of zone for scaling load 00263 */ 00264 void scaleLoadPower(double scale, int area, int zone); 00265 00266 /** 00267 * Return the total real power load for all loads in the zone. If zone 00268 * less than 1, then return the total load real power for the area 00269 * @param area index of area 00270 * @param zone index of zone 00271 * @return total load 00272 */ 00273 double getTotalLoadRealPower(int area, int zone); 00274 00275 /** 00276 * Return the current real power generation and the maximum and minimum total 00277 * power generation for all generators in the zone. If zone is less than 1 00278 * then return values for all generators in the area 00279 * @param area index of area 00280 * @param zone index of zone 00281 * @param total total real power generation 00282 * @param pmin minimum allowable real power generation 00283 * @param pmax maximum available real power generation 00284 */ 00285 void getGeneratorMargins(int area, int zone, double *total, double *pmin, 00286 double *pmax); 00287 00288 /** 00289 * Reset power of loads and generators to original values 00290 */ 00291 void resetPower(); 00292 00293 /** 00294 * Write real time path rating diagnostics 00295 * @param src_area generation area 00296 * @param src_zone generation zone 00297 * @param load_area load area 00298 * @param load_zone load zone 00299 * @param gen_scale scale factor for generation 00300 * @param load_scale scale factor for loads 00301 * @param file name of file containing diagnostics 00302 */ 00303 void writeRTPRDiagnostics(int src_area, int src_zone, int load_area, 00304 int load_zone, double gen_scale, double load_scale, const char *file); 00305 00306 /** 00307 * Get strings documenting contingency failures. Strings are *not* 00308 * terminated with a carriage return 00309 */ 00310 std::vector<std::string> getContingencyFailures(); 00311 00312 /** 00313 * User rate B parameter for line overload violations 00314 * @param flag if true, use RATEB parameter 00315 */ 00316 void useRateB(bool flag); 00317 00318 private: 00319 00320 // pointer to network 00321 boost::shared_ptr<PFNetwork> p_network; 00322 00323 // communicator for network 00324 gridpack::parallel::Communicator p_comm; 00325 00326 // pointer to factory 00327 boost::shared_ptr<PFFactoryModule> p_factory; 00328 00329 // maximum number of iterations 00330 int p_max_iteration; 00331 00332 // convergence tolerance 00333 double p_tolerance; 00334 00335 // qlim enforce flag 00336 int p_qlim; 00337 00338 // pointer to bus IO module 00339 boost::shared_ptr<gridpack::serial_io::SerialBusIO<PFNetwork> > p_busIO; 00340 00341 // pointer to branch IO module 00342 boost::shared_ptr<gridpack::serial_io::SerialBranchIO<PFNetwork> > p_branchIO; 00343 00344 // pointer to configuration module 00345 gridpack::utility::Configuration *p_config; 00346 00347 // string containing current contingency name 00348 std::string p_contingency_name; 00349 }; 00350 00351 } // powerflow 00352 } // gridpack 00353 #endif